--
-- Author: shineflag
-- Date: 2017-06-18 11:20:25
-- 消息中心，类似邮件系统

local skynet = require "skynet"
local proto = require("json_proto")
local json = require("json")
local log = require "log"
local cts = require("constants")

local TAG = "MSGBOX"

local BUGLE_ID = 12 --小喇叭的道具id

local user_center   --用户中心
local richdata    --道具中心

local CMD = {}

local mails = {   --所有的邮件
	-- [mid] = {
	-- 	{sender=1234,   --发件者
	-- 	 mtype="smsg",  --消息类型
	-- 	 title = "xxx", --消息标题 
	-- 	 content = "xxxx",  --消息内容
	-- 	 stime = 1412334,  --发件时间 unix时间戳
	-- 	}
	-- },
}

--所有的公告
local notices = {
	-- [1] = {
	-- 	title = "优惠大酬宾",
	-- 	content = "		在7月1号到15号期间,充100送100,充值玩家都有机会抽奖获取iphone8"
	-- },
}

--向所有在线的用户广播一个数据包
local function broadcast_pkg(pkg)
	skynet.send(user_center, "lua", "broadcast_online_user", pkg)
end

--向某个在线的用户广播一个数据包
local function push_user_pkg(mid,pkg)
	skynet.send(user_center, "lua", "send_user_pkg", mid,pkg)
end


local function change_bugle_by_broadcast(mid)
	return skynet.call(richdata, "lua", "changerid", mid, BUGLE_ID, -1, cts.pbugle)
end


--后台向在线的玩家广播消息 call by amdin
function CMD.admin_broad( mid,  mtype, title, content )
	local msg = {
		sender = mid, 
		mtype = mtype,
		title = title,
		content = content,
		stime = os.time()
	}

	log.d(TAG,"broadcast msg [%s]",json.encode(msg))
	local bpkg = proto.create_push("msgbox.live_broad",msg)
	broadcast_pkg(bpkg)
end

--向在线的玩家广播消息 call by admin
function CMD.pushmsg( data )
	local info = {
		mtype = data.mtype,
		msg = data.msg,
	}

	log.d(TAG,"pushmsg msg [%s]",json.encode(data))
	local bpkg = proto.create_push("msgbox.pushmsg",info)
	push_user_pkg(data.mid, bpkg)
end


-- call by userr agent
-- 获取消息数量
function CMD.mails_num(mid, data)
	local msgs = mails[mid]
	local num = msgs and #msgs or 0 
	return proto.create_resp("msgbox.mails_num",{ret=200,num=num})
end

-- call by userr agent
-- 获取消息数量
function CMD.recv(mid, data)
	local msgs = mails[mid]
	local msg_num = msgs and #msgs or 0 
	local resp =  {ret = 200, rnum=0, lnum=0,rmsgs = {}}
	local left_msg = {}
	if data.num <= 0 then 
		resp.rnum = msg_num 
		resp.lnum = 0
		resp.rmsgs = msgs or {}
	else
		resp.rnum = data.num 
		if data.num > msg_num then 
			resp.rnum = msg_num 
		end

		if resp.rnum > 0 then
			for k = 1, resp.rnum  do 
				resp.rmsgs[k] = msgs[k]
			end
			if msg_num > resp.rnum then 
				for k=resp.rnum ,msg_num do 
					table.insert(left_msg, msgs[k])
				end
			end
		end
	end
	if next(left_msg) then 
		mails[mid] = left_msg 
	else
		mails[mid] = nil 
	end

	return proto.create_resp("msgbox.recv",resp)
end


--向某用户发送邮件
function CMD.send(mid, data)
	local msg = {
		sender = mid, 
		mtype = data.mtype,
		title = data.title,
		content = data.content,
		stime = os.time()
	}

	local msgs = mails[data.recver]
	if not msgs then 
		msgs = {}
		mails[data.recver] = msgs
	end
	table.insert(msgs,msg)

	return proto.create_resp("msgbox.send",{ret =200})
end

--获取最新的通知
function CMD.newnotice()
	if not next(notices) then
		return proto.create_resp("msgbox.newnotice",{ret =-101})
	else
		return proto.create_resp("msgbox.newnotice",{ret =200, notice=notices[#notices]})
	end
end

function CMD.live_broad(mid, pkg)
	local left_bugle = change_bugle_by_broadcast(mid)

	local resp = {ret=200}
	if left_bugle  then 
		local data = proto.unpack_body(pkg)
		resp.left = left_bugle

		local msg = {
			sender = mid,
			mtype = data.req.mtype, 
			content = data.req.content,
			stime = os.time(),
		}
		log.d(TAG,"plyaer[%d] broadcast msg [%s]",mid, json.encode(msg))
		local bpkg = proto.create_push("msgbox.live_broad",msg)
		broadcast_pkg(bpkg)
	else
		resp.ret = -101 
	end

	return proto.create_resp("msgbox.live_broad",resp)
end

--发送公告
function CMD.pushnotice(tl, ct)
	local nt = {title = tl, content = ct}
	log.e(TAG,"push notice[%s]",json.encode(nt))
	table.insert(notices,nt)
end

--清除公告
function CMD.clearnotice()
	log.e(TAG,"clear notice")
	notices = {}
end


skynet.start(function()

	user_center = skynet.uniqueservice "user_mgr"
	richdata = skynet.uniqueservice "richdata"

	skynet.dispatch("lua", function(session, address, cmd, ...)
		local f = assert(CMD[cmd])
		if session ~= 0 then
			skynet.ret(skynet.pack(f(...)))
		else
			f( ... )
		end
	end)

end)
